package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.kzgs.KzgsResultJson;
import com.app.domain.kzgs.Record;
import com.app.domain.kzgs.RecordDetails;
import com.app.domain.mslc.MslcResultJson;
import com.app.domain.mslc.ReceiveYJData;
import com.app.domain.mslc.SjkYjDetai;
import com.app.domain.mslc.Token;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class MslcService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;

    public ReceiveYJData getRecord(List<YjData> yjDataList, UploadParameter uploadParameter) {
        ReceiveYJData receiveYJData=new ReceiveYJData();
        receiveYJData.setReceiveId(UUID.randomUUID().toString().replace("-",""));
        receiveYJData.setWbsCode(uploadParameter.getUuid());
        receiveYJData.setBeamType(uploadParameter.getLblx());
        receiveYJData.setComponentId(yjDataList.get(0).getDevice_code());
        receiveYJData.setComponentParts(yjDataList.get(0).getBridge_code());
        receiveYJData.setEngineeringName(uploadParameter.getGcmc());
        receiveYJData.setEngineeringSite(uploadParameter.getSgbw());
        receiveYJData.setEquNum(yjDataList.get(0).getDevice_code());
        receiveYJData.setBridgeName(uploadParameter.getZhbw());
        List<SjkYjDetai> sjkYjDetaiList=new ArrayList<>();
        for (YjData yjData : yjDataList) {
            SjkYjDetai sjkYjDetai=new SjkYjDetai();
            sjkYjDetai.setSteelBeamNo(yjData.getDuct_code());
            sjkYjDetai.setPulpingOrientation(uploadParameter.getYajiangfangxiang());
            sjkYjDetai.setPulpingOrder(uploadParameter.getYajiangbuzhou());
            sjkYjDetai.setFlowVelocity(yjData.getPractical_fluidity());
            sjkYjDetai.setFluidity(yjData.getPractical_fluidity());
            sjkYjDetai.setMixProportion(uploadParameter.getPhb());
            sjkYjDetai.setWaterRatio(String.valueOf(yjData.getWater_binder_ratio()));
            sjkYjDetai.setStirringTime(uploadParameter.getStirtime());
            sjkYjDetai.setPulpingVolume(String.valueOf(yjData.getPractical_volume()));
            sjkYjDetai.setStandardVolume(String.valueOf(yjData.getTheory_volume()));
            sjkYjDetai.setPulpingPressureIn(String.valueOf(yjData.getMudjack_pressure()));
            sjkYjDetai.setPulpingPressureOut(String.valueOf(yjData.getSlurry_pressure()));
            sjkYjDetai.setHoldingTime(yjData.getPressurize_time());
            sjkYjDetai.setStartDate(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            sjkYjDetai.setEndDate(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            sjkYjDetaiList.add(sjkYjDetai);
        }
        receiveYJData.setSjkYjDetailList(sjkYjDetaiList);
        return receiveYJData;
    }

    public Token getToken(String url, String authorization, String tenant_id) {
        String result = HttpClients.requestPost(url, authorization, tenant_id);
        Token token = (Token) JSON.parseObject(StringUtil.replaceBlank(result), Token.class);
        return token;
    }


    public MslcResultJson sendMessage(String serviceUrl, String json,String token) {
        String result = HttpClients.msclRequestPost(serviceUrl, json,token);
        MslcResultJson resultJson = (MslcResultJson) JSON.parseObject(StringUtil.replaceBlank(result), MslcResultJson.class);
        return resultJson;
    }



}
